package com.ecommerce.repository;
import com.ecommerce.entity.Cart;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;

@Repository
public interface CartRepository extends JpaRepository<Cart, Long> {
    // 根据用户ID查询购物车
    List<Cart> findByUserId(Long userId);

    // 根据用户ID和商品ID查询购物车项
    Cart findByUserIdAndProductId(Long userId, Long productId);

    // 删除用户的所有购物车项
    @Modifying
    @Transactional
    @Query("DELETE FROM Cart c WHERE c.userId = :userId")
    void deleteByUserId(Long userId);
}